﻿/****************************** Module Header ******************************\
* Plus Name:  Website.js
* Plus Version:  1.0.0 (2011-12-9)
* Copyright (c) JianDanMai.Com (14507247@qq.com)
* 
* This plus used to initialize page load event for this website.
* 
* This source is subject to the GPL License.
* See http://www.gnu.org/licenses/gpl.html.
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

// declare a global variable for this website.
var page = {};
page.MessageBox = {};

// page initialize function
// Warning: Include this document must after include jquery plus and jquery-ui plus.
jQuery(document).ready(function ($) {
    // function here...
    // 检测用户站内信
    //$("#NewPM").load("/Ajax/CheckPM.aspx?uid=0");
    //$("#Calendar,.dateInput").datepicker({ changeMonth: true, showWeek: true, dateFormat: 'yy-mm-dd' });
    // 标准表单扩展域控制开关
    $(".formlist-extend-hd a").click(function () {
        $(this).text(($(this).text() == "[点击打开]") ? "[点击关闭]" : "[点击打开]");
        $(this).parent().parent().parent().next().toggle();
    });

    // 打开桌面按钮事件
    var deskTopState = false;
    $("#lnkDesktop").click(function () {
        var $dt = $("#divDesktop").dialog({ width: '50%' });
        $dt.dialog(deskTopState ? 'close' : 'open');
        deskTopState = $dt.dialog('isOpen');
    });

    // 自增按钮点击
    $('.btnIncrement').click(function () {
        $tb = $(this).prev();
        $tb.val($tb.val() * 1 + 1);
    });

    // 自减按钮点击
    $('.btnDecrement').click(function () {
        $tb = $(this).next();
        if ($tb.val() * 1 < 2) return;
        $tb.val($tb.val() * 1 - 1);
    });

    // 验证码图片切换按钮点击事件 2012-4-27
    $('.btnCaptcha').click(function () {
        var $img = $(this).prev();
        $img.attr('src', $img.attr('src') + '?r=' + Math.random());
    });

});

// ============Here are the needfull methods of this website=========

// version
page.version = "1.0.0 (2011-12-9)";

// author
page.author = "Yoin.CN (14507247@qq.com)";

// other method here...

// check personal message
// page.CheckPM();
// 弹出页面消息窗口
// Version:2011-12-26
// e.g. <input type="button" value="Show" onclick="page.ShowNormalMessage('消息内容','消息标题','http://www.baidu.com',true);" />
page.MessageBox.Show = function (strMessage, strTitle, strAction, isModal) {
    var $messageBox = $("<div>" + strMessage + "</div>");
    $messageBox.dialog({ autoOpen: false });
    if (strTitle) $messageBox.dialog({ title: strTitle });
    if (strAction) {
        switch (strAction) {
            case "":
                $messageBox.dialog({ buttons: { '确定': function () { $(this).dialog('close'); } } });
                break;
            case "history.back":
                $messageBox.dialog({ buttons: { '确定': function () { parent.history.back(); } } });
                break;
            default:
                $messageBox.dialog({ buttons: { '确定': function () { location.href = strAction; } } });
                break;
        }
    }
    if (isModal) $messageBox.dialog({ modal: true });
    $messageBox.dialog("open");
}
// 获取上传控件真实路径，兼容IE7,IE8,FF
page.GetUploadControlPath = function (obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            return document.selection.createRange().text;
        } else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
};

// 使用滤镜进行本地图片预览
page.PreviewImg = function (previd, filepath) {
    var newPreview = document.getElementById(previd);
    $(newPreview).attr("style", "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);");
    newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = filepath;
    newPreview.style.width = "120px";
    newPreview.style.height = "120px";
};

// 设置文本控件的默认值
page.SetInputControlDefaultValue = function ($Input, strValue) {
    if (!$Input[0]) return;
    var strVal = $.trim($Input.val());
    if (strVal.length > 0) return;
    $Input.val(strValue);
    $Input.attr("style", "color:#CCC;");
    $Input.focus(function () {
        $Input.val("");
        $Input.attr("style", "");
    });
    $Input.blur(function () {
        if (!$.trim($Input.val()).length) {
            $Input.val(strValue);
            $Input.attr("style", "color:#CCC;");
        }
    });
}

// 设置容器高度
// e.g. page.SetContentHeight(".fbody", 0, ".header", ".footer");
page.SetContentHeight = function (contentIndex, padHeight, arrDomIndexs) {
    if (arguments.length < 3) return;
    var $content = $(contentIndex);
    if (!$content[0]) return;
    var iAllHeight = 0;
    var oDom = null;
    for (var i = 2; i < arguments.length; i++) {
        oDom = $(arguments[i]);
        if (oDom[0]) iAllHeight += oDom.height();
    }
    var height = document.body.clientHeight - iAllHeight;
    if ((height - $content.height()) < 0) return;
    $content.height(height);
}

// 设置日历
// e.g. page.SetCalendar("#ctl00_ContentPlaceHolder1_calDueDate");
page.SetCalendar = function (inputIndex) {
    var $input = $(inputIndex);
    if (!$input[0]) return;
    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
    $input.datepicker({ changeMonth: true, showWeek: true });
}

// 浏览器右上角消息提示
page.FlyOut = function (message, color, func) {
    if (!color) var color = '#FFF';
    var $o = $('<div>' + message + '</div>').css({ 'position': 'absolute', 'top': '0px', 'right': '0px', 'color': color });
    $o.appendTo(document.body);
    $o.animate({ top: '-50', opacity: 0.1 }, 2000, function () { if (!func) return; func; });
}

// 设置时间

page.Clock = function (format) {
    if (!format) var format = 'yyyy-MM-dd HH:mm:ss';
    var $o = {};
    var now = new Date();
    $o.yyyy = now.getYear();
    $o.M = now.getMonth() + 1;
    $o.d = now.getDate();
    $o.H = now.getHours();
    $o.m = now.getMinutes();
    $o.s = now.getSeconds();
    $o.w = now.getDay();
    $o.MM = $o.M < 10 ? "0" + $o.M : $o.M;
    $o.dd = $o.d < 10 ? "0" + $o.d : $o.d;
    $o.HH = $o.H < 10 ? "0" + $o.H : $o.H;
    $o.mm = $o.m < 10 ? "0" + $o.m : $o.m;
    $o.ss = $o.s < 10 ? "0" + $o.s : $o.s;
    $o.W = new Array("天", "一", "二", "三", "四", "五", "六")[$o.w];
    $o.str = format.replace('yyyy', $o.yyyy);
    $o.str = $o.str.replace('MM', $o.MM);
    $o.str = $o.str.replace('dd', $o.dd);
    $o.str = $o.str.replace('HH', $o.HH);
    $o.str = $o.str.replace('mm', $o.mm);
    $o.str = $o.str.replace('ss', $o.ss);
    $o.str = $o.str.replace('W', $o.W);
    return $o.str;
}

// 加入收藏
// Demo:<a onclick="AddFavorite(window.location,document.title)" style="cursor:pointer">加入收藏</a> 
page.AddFavorite = function (url, title) {
    try {
        window.external.addFavorite(url, title);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

// 设为首页
// Demo:<a onclick="SetHome(this,window.location)" style="cursor:pointer">设为首页</a>
page.SetHomePage = function (obj, url) {
    if (!url) url = window.location;
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(url);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', url);
        }
    }
}